package com.sjd.web.controller.mall;

import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.mall.domain.dto.goods.SjdWebSiteGoodsCommentQueryRequestDTO;
import com.sjd.mall.domain.dto.goods.SjdWebSiteGoodsCommentQueryResponseDTO;
import com.sjd.mall.service.goods.GoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 商品评论controller
 * @date : 2022-01-20 22:44
 */
@RestController
@RequestMapping("mall/comment")
public class GoodsCommentController extends BaseController {

    @Autowired
    private GoodsCommentService commentService;

    /**
     * 查询评论
     * @param requestDTO requestDTO
     * @return return
     */
    @GetMapping()
    //@PreAuthorize("@ss.hasPermi('mall:goodsComment:list')")
    public Object queryComments(@Valid SjdWebSiteGoodsCommentQueryRequestDTO requestDTO , BindingResult results) {
        if (results.hasErrors()) {
            return AjaxResult.error(Objects.requireNonNull(results.getFieldError()).getDefaultMessage());
        }
        startPage();
        List<SjdWebSiteGoodsCommentQueryResponseDTO> responseLists = commentService.queryCommentWebsite(requestDTO);
        return getDataTable(responseLists);
    }

    /**
     * 查询评论
     * @param goodsId goodsId
     * @return return
     */
    @GetMapping("goods-comment/{goodsId}")
    //@PreAuthorize("@ss.hasPermi('mall:goodsComment:list')")
    public Object queryCommentsByGoodsId(@PathVariable("goodsId") Long goodsId) {
        startPage();
        List<SjdWebSiteGoodsCommentQueryResponseDTO> responseLists = commentService.queryCommentWebsiteByGoodsId(goodsId);
        return getDataTable(responseLists);
    }

    /**
     * 删除评论
     * @param commentId commentId
     * @return return
     */
    @PostMapping("delete/{commentId}")
    //@PreAuthorize("@ss.hasPermi('mall:goodsComment:delete')")
    public AjaxResult queryComments(@PathVariable("commentId") Long commentId){
         return commentService.delComment(commentId);
    }
}
